package com.boge.modules.shop.service.impl;

import com.boge.modules.shop.entity.GoodsEntity;
import com.boge.modules.shop.entity.GoodsnewEntity;
import com.boge.modules.shop.service.GoodsService;
import com.boge.modules.shop.service.GoodsnewService;
import com.boge.modules.sys.controller.WebController;
import com.boge.modules.sys.entity.SysUserEntity;
import com.boge.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boge.common.utils.PageUtils;
import com.boge.common.utils.Query;

import com.boge.modules.shop.dao.CarDao;
import com.boge.modules.shop.entity.CarEntity;
import com.boge.modules.shop.service.CarService;


@Service("carService")
public class CarServiceImpl extends ServiceImpl<CarDao, CarEntity> implements CarService {

    @Autowired
    private SysUserService userService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsnewService goodsnewService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CarEntity> page = this.page(
                new Query<CarEntity>().getPage(params),
                new QueryWrapper<CarEntity>()
        );

        List<CarEntity> records = page.getRecords();
        if(records != null && records.size() > 0){
            for (CarEntity record : records) {
                Integer userId = record.getUserId();
                SysUserEntity user = userService.getById(userId);
                record.setUserName(user.getUsername());

                Integer goodsId = record.getGoodsId();
                GoodsEntity goods = goodsService.getById(goodsId);
                if(goods != null){
                    record.setGoodsName(goods.getTitle());
                    record.setImg(WebController.BASE_URL+goods.getImage());
                    record.setPrice(goods.getPrice());
                    record.setDesc(goods.getDescription());
                }else{
                    GoodsnewEntity goodsnew = goodsnewService.getById(goodsId);
                    if(goodsnew !=null){
                        record.setGoodsName(goodsnew.getComment());
                        record.setImg(WebController.BASE_URL+goodsnew.getImg());
                        record.setPrice(goodsnew.getPrice());
                        record.setDesc(goodsnew.getDes());
                    }
                }
            }
        }

        return new PageUtils(page);
    }

}